<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form :model="resourceData" v-loading="isLoading">
      <el-form-item label="资源名称" :label-width="formLabelWidth">
        <el-input v-model="resourceData.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="资源路径" :label-width="formLabelWidth">
        <el-input v-model="resourceData.url" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="资源分类" :label-width="formLabelWidth">
        <el-select
          v-model="resourceData.categoryId"
          placeholder="请选择资源分类"
        >
          <el-option
            v-for="item in categorys"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述" :label-width="formLabelWidth">
        <el-input
          type="textarea"
          v-model="resourceData.description"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit" :disabled="isLoading"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
import { getAllResourceCategory } from '@/api/resource-category'
import { getResource, resourceSaveOrUpdate } from '@/api/resource'

export default {
  name: 'ResourceCreateOrUpdate',
  data () {
    return {
      isLoading: false,
      isCreate: true,
      dialogFormVisible: false,
      resourceData: {
        id: undefined,
        name: '',
        url: '',
        categoryId: 1,
        description: ''
      },
      formLabelWidth: '120px',
      categorys: []
    }
  },
  computed: {
    title () {
      return this.isCreate ? '新建资源' : '编辑资源'
    }
  },
  methods: {
    onSubmit () {
      resourceSaveOrUpdate(this.resourceData)
        .then((res) => {
          if (res.data.data) {
            this.$emit('resource-change')
            this.$message.success(`${this.title}成功~`)
          } else {
            this.$message.error(`${this.title}失败~`)
          }
        })
        .finally(() => {
          this.dialogFormVisible = false
        })
    },
    showAndInit (id = 0) {
      this.dialogFormVisible = true
      this.isCreate = id < 1
      getAllResourceCategory().then((res) => {
        if (res.data.code === '000000') {
          this.categorys = res.data.data
        } else {
          this.$message.error('获取资源类别失败')
        }
      })
      if (this.isCreate) {
        // 新建
        this.resourceData = {
          id: undefined,
          name: '',
          url: '',
          categoryId: 1,
          description: ''
        }
      } else {
        // 编辑
        this.isLoading = true
        getResource(id)
          .then((res) => {
            if (res.data.code === '000000') {
              this.resourceData = res.data.data
            } else {
              this.$message.error('获取资源失败')
            }
          })
          .finally(() => {
            this.isLoading = false
          })
      }
    }
  }
}
</script>
<style scoped lang="scss">
</style>
